package org.rendao.auth.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.rendao.auth.assemble.UserDetailAssembler;
import org.rendao.auth.dao.UserDetailDao;
import org.rendao.auth.data.UserDetailDO;
import org.rendao.auth.model.UserDetail;
import org.rendao.auth.service.UserDetailService;
import org.rendao.constant.ConstantDefinition;
import org.rendao.exception.TBFError;
import org.rendao.exception.TBFException;
import org.rendao.utils.AssembleUtils;
import org.rendao.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

public class UserDetailServiceImpl implements UserDetailService {

	@Autowired
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Autowired
	private NamedParameterJdbcTemplate jdbcTemplate;
	
	public void setJdbcTemplate(NamedParameterJdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	@Autowired
	private UserDetailDao userDao;

	public void setUserDao(UserDetailDao userDao) {
		this.userDao = userDao;
	}
	
	@Transactional
	@SuppressWarnings("unchecked")
	@Override
	public List<UserDetail> getAll() {
		
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(UserDetailDO.class);
		Criterion criterion = Restrictions.eq("isDeleted", ConstantDefinition.IS_DELETED_FALSE);
		criteria.add(criterion);
		List<UserDetailDO> users = criteria.list();
		List<UserDetail> result = new ArrayList<UserDetail>();
		for(UserDetailDO userDO : users) {
			UserDetail user = UserDetailAssembler.doToModel(userDO);
			result.add(user);
		}
		return result;
	}

	@Override
	public UserDetail getById(String id) {
		UserDetailDO detailDO = userDao.getById(id);
		return UserDetailAssembler.doToModel(detailDO);
	}

	@Override
	public UserDetail saveOrUpdate(UserDetail userDetail) {
		UserDetailDO userDetailDO = UserDetailAssembler.modelToDo(userDetail);
		UserDetailDO result = null;
		if(StringUtils.isNullOrEmpty(userDetail.getId())) {
			result = userDao.save(userDetailDO);
		}
		else {
			result = userDao.update(userDetailDO);
		}
		return UserDetailAssembler.doToModel(result);
	}

	@Transactional
	@Override
	public int deleteById(String id) {
		String sql = "select count(*) from question where user_id = :id";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		int count = jdbcTemplate.queryForInt(sql, params);
		if(count > 0) {
			throw new TBFException(TBFError.ASSOCIATION_DELETE_DENY);
		}
		return userDao.softDelete(id);
	}

	@Override
	@Transactional
	public UserDetail getByName(String name) {
		String hql = "from UserDetailDO u where u.username = :username and " +
				"u.isDeleted = :isDeleted";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter("username", name);
		query.setParameter("isDeleted", ConstantDefinition.IS_DELETED_FALSE);
		UserDetailDO user = (UserDetailDO)query.uniqueResult();
		return UserDetailAssembler.doToModel(user);
	}

}
